package com.ksp.wke.kspt02;
import static com.ksp.com.KSPCOM_BasConst.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksp.dom.KSPDOM_ResDta;
import com.ksp.dom.KSPDOM_TabDta;
import com.ksp.dom.KSPDOM_User;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONValue;

/**
 * 用户任务列表查询
 * @author anye
 *
 */
@Controller
public class KSPT02Controller {

	@Autowired
	private KSPT02Mapper kspt02Mapper;

	@RequestMapping("/tsk/KSPT02")
	@ResponseBody
	public KSPDOM_ResDta iniPam(ServletRequest request) {
		String wCooTkn = request.getParameter(COO_TKN);

		//判断登录用户
		HttpServletRequest hreq = (HttpServletRequest)request;
		Object wPamUsr = hreq.getSession().getAttribute(SES_TKN + ":" + wCooTkn);
		KSPDOM_User curUsr  = (KSPDOM_User)hreq.getSession().getAttribute(USR_OAM);
		String wCurOam = curUsr.getUsroam();
		if (!wCurOam.equals(wPamUsr)) {
			return KSPDOM_ResDta.buildFail("当前用户异常");
		}

		//
		Map<String, Object> wPamMap = new HashMap<>();
		wPamMap.put("QUA_USR", wCurOam);
		KSPDOM_ResDta wResDta = new KSPDOM_ResDta();
		try {
			wResDta = exeSrv(wPamMap);
		} catch (Exception e) {
			wResDta.setCod("400");
			wResDta.setMsg(e.getMessage());
		}
		return wResDta;
	}

	public KSPDOM_ResDta exeSrv(Map<String, Object> pPamMap) {
		List<Map<String, Object>> wQryQuaLst = kspt02Mapper.qryUsrTsk(pPamMap);

		return KSPDOM_ResDta.buildSucc(JSONArray.toJSONString(wQryQuaLst));
	}
	
	/**
	 * 列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/tsk/KSPT02B")
	@ResponseBody
	public KSPDOM_TabDta iniPam2(ServletRequest request) {
		String wCooTkn = request.getParameter(COO_TKN);
		String WKspCon = request.getParameter("KSP_CON");
		String wDtaPge = request.getParameter("page");
		String wDtaLmt = request.getParameter("limit");
		int wDtaPgeInt = Integer.parseInt(wDtaPge);
		int wDtaLmtInt = Integer.parseInt(wDtaLmt);
		wDtaPgeInt = (wDtaPgeInt-1) * wDtaLmtInt; 

		//判断登录用户
		HttpServletRequest hreq = (HttpServletRequest)request;
		Object wPamUsr = hreq.getSession().getAttribute(SES_TKN + ":" + wCooTkn);
		KSPDOM_User curUsr  = (KSPDOM_User)hreq.getSession().getAttribute(USR_OAM);
		if (curUsr == null) {
			KSPDOM_TabDta errDta = new KSPDOM_TabDta();
			errDta.setCode(1);
			errDta.setCount(0);
			errDta.setMsg("请先登录");
			errDta.setData(null);
			return errDta;
		}
		String wCurOam = curUsr.getUsroam();

		//
		Map<String, Object> wPamMap = new HashMap<>();
		wPamMap.put("KSP_CON", WKspCon);
		wPamMap.put("QUA_USR", wCurOam);
		
		wPamMap.put("DTA_LOC", wDtaPgeInt);
		wPamMap.put("DTA_SIZ", wDtaLmtInt);
		KSPDOM_TabDta wResDta = new KSPDOM_TabDta();
		try {
			wResDta = exeSrv2(wPamMap);
		} catch (Exception e) {
			wResDta.setCode(1);
			wResDta.setMsg(e.getMessage());
		}
		return wResDta;
	}

	public KSPDOM_TabDta exeSrv2(Map<String, Object> pPamMap) {
		KSPDOM_TabDta wTskTblDta = new KSPDOM_TabDta();
		Map<String, Object> wUsrMap = new HashMap<>();
		wUsrMap.put("USR_OAM", pPamMap.get("QUA_USR"));
		List<Map<String, Object>> wUsrInfLst = kspt02Mapper.qryUsrInfo(wUsrMap);
		pPamMap.put("USR_TYP", wUsrInfLst.get(0).get("USR_TYP"));
		
		List<Map<String, Object>> wUsrTskTotalLst = kspt02Mapper.qryUsrTskTotal(pPamMap);
		int total = Integer.parseInt(wUsrTskTotalLst.get(0).get("TOL_CNT").toString());
		
		List<Map<String, Object>> wUsrTskTblLst = kspt02Mapper.qryUsrTskTbl(pPamMap);
		
		wTskTblDta.setCode(0);
		wTskTblDta.setCount(total);
		wTskTblDta.setMsg("");
		wTskTblDta.setData(wUsrTskTblLst.toArray());
		return wTskTblDta;
	}
}
